package tech.aistar.controller.noteshow;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import tech.aistar.entity.authordis.NoteAuthor;
import tech.aistar.entity.authordis.UserTable;
import tech.aistar.entity.daoyou.GuideCodeStatus;
import tech.aistar.entity.daoyou.GuideResult;
import tech.aistar.entity.notepay.NoteOrder;
import tech.aistar.entity.person_center.Orders;
import tech.aistar.service.ordershow.IOrderService;
import tech.aistar.service.personinfo.IPersonInfoService;
import tech.aistar.util.UploadImg.QiniuCloudUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Dell on 2019/10/1.
 */
@Controller
@SuppressWarnings("all")
public class PersonCenter {

    @Autowired
    private IOrderService orderService;
    @Autowired
    private IPersonInfoService personInfoService;



    //为了实现异步分页..
    @RequestMapping("/person")
    public String personCenter(HttpServletRequest request,Model model){
        HttpSession session=request.getSession();
        UserTable userTable= (UserTable) session.getAttribute("userId");
        //用户放进作用域,方便头像,用户名等信息回显
        model.addAttribute("userTable",userTable);
        return "person_center/person_center";
    }


    /**
     * @Description:个人中心页面
     * @param:
     * @author XiaoDi
     * @date 2019/10/7 19:32
    */
    @RequestMapping("/person-center")
    public String findTotalPage(HttpServletRequest request, Model model,Integer order_status, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "2") Integer pageSize){

        //把订单状态放进作用域,以便于查询
        model.addAttribute("order_status",order_status);

        //登录之后,拿到userTable
        HttpSession session=request.getSession();
        UserTable userTable= (UserTable) session.getAttribute("userId");
        //用户放进作用域,方便头像,用户名等信息回显
        model.addAttribute("userTable",userTable);

       //导游订单数据回显
       PageInfo<Orders> pageInfo = orderService.findAll(userTable.getId(),order_status,Integer.valueOf(pageNum),Integer.valueOf(pageSize));
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("user_id",userTable.getId());


       //个人信息回显=========


        // 查询信息person回显到页面
        UserTable personInfo = personInfoService.getById(userTable.getId());
        //把对象放进作用域中
        model.addAttribute("personInfo",personInfo);

        //头像等信息回显
        NoteAuthor noteAuthor = personInfoService.findUserInfoById(userTable.getId());
        model.addAttribute("noteAuthor",noteAuthor);

        //文章信息
        List<NoteOrder> noteOrder = personInfoService.findNoteOrder(userTable.getId());

        model.addAttribute("noteOrder",noteOrder);

        return "person_center/person_order_data";
}

    /**
     * @Description:点击修改个人信息  到修改个人信息页面
     * @param:
     * @author XiaoDi
     * @date 2019/10/4 0:43
    */
    @PostMapping("/person-info")
    public String personInfo(){
        //使用button按钮sumbit...
        //  接收post请求,重定向页面到修改信息页面
        return "redirect:update-personInfo";

    }
    //还可以使用button按钮button,直接到这个方法
    //到修改个人信息页面
    @GetMapping("/update-personInfo")
    public String updatePersonInfo(Model model,HttpServletRequest request){
        //登录之后,拿到userTable
        HttpSession session=request.getSession();
        UserTable userTable= (UserTable) session.getAttribute("userId");
        //用户放进作用域,方便头像,用户名等信息回显
        model.addAttribute("userTable",userTable);


        //把对象放进作用域中
        UserTable personInfo = personInfoService.getById(userTable.getId());
        model.addAttribute("personInfo",personInfo);


        return "person_center/person_info";
    }
    /**
     * @Description: 修改完成个人信息 -- 点击保存之后
     * @param:重定向到后台
     * @author XiaoDi
     * @date 2019/10/1 18:57
     */
    @PostMapping("/update")
    public String updatePersonInfo(UserTable userTable){

        personInfoService.saveUserTable(userTable);

        return "person_center/person_info_finish";
    }



    /**
     * @Description:ajax实现异步请求,实现查询预约订单的页面
     * @param:
     * @author XiaoDi
     * @date 2019/10/2 21:33
     */
    @RequestMapping("/book_order")
    public String findBookOrder(Model model, UserTable userTable, String order_status, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "2") Integer pageSize){
        //模拟数据
        userTable.setId(5);

//        Integer user_id = userTable.getId();
        Integer user_id = 1;
//        订单数据回显
        PageInfo<Orders> pageInfo = orderService.findAll(user_id,0,Integer.valueOf(pageNum),Integer.valueOf(pageSize));
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("user_id",user_id);

//        个人信息回显=========
        //把usertable信息更新到person里面
//        personInfoService.saveUserInfo(userTable);

//        查询信息person回显到页面

       UserTable personInfo = personInfoService.getById(userTable.getId());
        //把对象放进作用域中
        model.addAttribute("personInfo",personInfo);



        return "person_center/person_center";
    }

    @RequestMapping("/uploadHeadImg")
    @ResponseBody
    public Map uploadHeadImg(MultipartFile myfile) {
        Map<String,String> map = new HashMap<>();

        try {
            String imgUrl = QiniuCloudUtil.upload(myfile.getBytes(),myfile.getOriginalFilename());
            map.put("imgUrl", imgUrl);

        } catch (IOException e) {
            e.printStackTrace();
            map.put("imgUrl", null);

        }
        //因为要作为结果返回
        return map;

    }

    @RequestMapping("/head")
    public String headImg(HttpServletRequest request,Model model){
        //登录之后,拿到userTable
        HttpSession session=request.getSession();
        UserTable userTable= (UserTable) session.getAttribute("userId");
        model.addAttribute("userTable",userTable);
        return "person_center/person_uploadimg";
    }

    @RequestMapping("/saveheadImg")
    @ResponseBody
    public GuideResult saveSuccessHead(Integer id, String photo){

        personInfoService.saveHeadImg(id,photo);

        if (id != null){
            return new GuideResult(GuideCodeStatus.OK,true,"保存成功");
        }else {
            return new GuideResult(GuideCodeStatus.OK,false,"保存失败");
        }
    }


}
